<template>
  <div>
    <FormPanel
      ref="formPanel"
      :list="formList"
      :cols="2"
      labelWidth="100"
      :isSubmitBtn="false"
      :formType="type"
      @formChange="formChangeHandle"
    />
    <div style="text-align: center;margin: 20px 0 20px 0;">
      <el-button size="small" @click="closeHandler">取消</el-button>
      <el-button size="small" type="primary" @click="saveHandle">保存</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getDataType } from '@/assets/js/auth';
import { queryClueSource, addClueSource, editClueSource } from '@/api/potenCus/basedata/cueSourceDefinition';
import { getAppId } from '@/assets/js/auth';

export default {
  name: 'SourceDetail',
  mixins: [dictionary],
  props: {
    type: '',
    rowData: ''
  },
  data() {
    return {
      formList: this.createFormList(),
      upSourceList: [],
      saveData: null
    };
  },
  mounted() {
    const { rowData, type } = this;
    if (type === 'EDIT' && rowData.clueLevel !== 1) {
      let params = {
        isEnable: 10011003,
        level: rowData.clueLevel - 1, // 只有查询一级树有值：1
        upId: '' // 带上当前展开树的id
      };
      this.init(params);
    } else if (type === 'ADD' && rowData.clueLevel >= 1) {
      let params = {
        isEnable: 10011003,
        level: rowData.clueLevel, // 只有查询一级树有值：1
        upId: '' // 带上当前展开树的id
      };
      this.init(params);
    } else {
      this.fillInitData();
    }
  },
  methods: {
    // 获取上级来源
    async init(params) {
      let { rowData, type } = this;
      await queryClueSource(params).then(res => {
        let treeData = res.data;
        if (res.success) {
          // 筛选
          let orginList = treeData.filter(item => item.dataSources == rowData.dataSources);
          // 整形
          this.upSourceList = orginList.map(item => {
            return {
              text: item.clueName,
              value: item.id
            };
          });
        }
      });
      // setTimeout(()=>{
      this.fillInitData();
      // },0)
    },
    fillInitData() {
      const { type, rowData } = this;
      this.formList.forEach(item => {
        if (type === 'ADD') {
          if (item.fieldName === 'upClueId') {
            item.initialValue = rowData['id'] || null;
            item.itemList = this.upSourceList;
          }
          if (item.fieldName === 'isEnable') {
            item.initialValue = 10011001;
          }
          if (rowData.clueLevel >= 1) {
            // 新增子类 >= 1
            if (item.fieldName === 'dataType') {
              item.initialValue = rowData['dataType'];
              item.disabled = true;
              item.rules = '';
            }
          }
        } else if (type === 'EDIT') {
          if (item.fieldName === 'upClueId') {
            item.initialValue = rowData['upClueId'];
            if (rowData.clueLevel > 1) {
              // 编辑子类 > 1
              item.itemList = this.upSourceList;
            }
          } else if (item.fieldName === 'dataType') {
            item.initialValue = rowData['dataType'];
            // if(rowData.clueLevel > 1){ // 编辑子类
            item.disabled = true;
            item.rules = '';
            // }
          } else if (item.fieldName === 'sourseAppId') {
            debugger
            item.initialValue = rowData['sourseAppId'];
            if(item.initialValue===''||item.initialValue===null||item.initialValue===undefined){ // 编辑子类
              item.disabled = false;
            }else{
              item.disabled = true;
            }
          } else {
            item.initialValue = rowData[item.fieldName];
          }
        }
      });
    },
    createFormList() {
      let { rowData, type } = this;
      let dataType = getDataType('dataType'); // 车厂(10461003)、经销商
      // 经销商不能选择线上，只能线下
      // let dataTypeList = dataType.toString() == '10461001' ? this.createDictList('7026').filter(item => {return item.value == 70261002}) : this.createDictList('7026')
      //20191204处理CYX-16054
      let dataTypeList = this.createDictList('7026');
      let isHide = false;
      if (getAppId() == 'arcfox') {
        isHide = true;
      }
      let list1 = [
        {
          type: 'SELECT',
          label: '上级来源',
          fieldName: 'upClueId',
          initialValue: '',
          offsetRightCols: 1,
          itemList: []
        }
      ];
      let list2 = [
        {
          type: 'INPUT',
          label: '来源名称',
          fieldName: 'clueName',
          offsetRightCols: 1,
          rules: [{ required: true, message: '请输入来源名称', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '描述',
          fieldName: 'remark',
          offsetRightCols: 1,
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '数据类型',
          fieldName: 'dataType',
          disabled: this.type === 'edit' ? true : false,
          offsetRightCols: 1,
          rules: [{ required: true, message: '请选择数据类型', trigger: 'blur' }] || '',
          itemList: dataTypeList
        },
        {
          type: 'SELECT',
          label: '渠道类型',
          fieldName: 'channelType',
          rules: [{ required: true, message: '请选择渠道类型', trigger: 'change' }] || '',
          offsetRightCols: 1,
          hidden: isHide,
          initialValue: '',
          itemList: this.createDictList('2589')
        },
        // {
        //   type: 'INPUT',
        //   label: 'APPID',
        //   // disabled: this.type === 'EDIT' ? true : false,
        //   fieldName: 'sourseAppId',
        //   // disabled: props.rowData.sourseAppId === '' ? false : true,
        //   offsetRightCols: 1,
        //   // rules: [{
        //   // validator:(row, val, callback) => {
        //   // const reg = /^[A-Z]+$/;
        //   // console.log(val);
        //   // if (!reg.test(val)) {
        //   //  return callback(new Error('请填写大写字母'));

        //   // } else {
        //   // callback();
        //   // }
        //   // }
        //   // }],
        //   rules: [{ pattern: /^[A-Z]+$/, message: '请填写大写字母', trigger: 'blur' }],
        //   initialValue: ''
        // },
        // {
        //   type: 'INPUT',
        //   label: 'APPKEY',
        //   fieldName: 'appKey',
        //   offsetRightCols: 1,
        //   disabled: true,
        //   initialValue: ''
        // },
        {
          type: 'CHECKBOX',
          label: '是否有效',
          fieldName: 'isEnable',
          initialValue: '',
          itemList: this.createDictList('1001'),
          options: {
            trueValue: 10011001,
            falseValue: 10011002
          }
        }
      ];
      let result = [];
      if ((type === 'EDIT' && rowData.clueLevel !== 1) || (type === 'ADD' && rowData.clueLevel >= 1)) {
        result = [].concat(list1, list2);
      } else {
        result = [].concat(list2);
      }
      return result;
    },
    addFunc() {
      debugger
      let { rowData } = this;
      // debugger
      this.$refs.formPanel.SUBMIT_FORM();
      // const reg = /^[A-Z]+$/;
      // if (!reg.test(this.saveData.sourseAppId)) {
      //   // this.$notify.error({ title: '提示信息', message: '请填写大写字母' });
      //   return;
      // }
      let params = {
        id: null,
        clueLevel: rowData.clueLevel + 1,
        dataSources: null, //
        ownerCode: null, //
        clueName: this.saveData.clueName, // TODO
        dataType: this.saveData.dataType, // TODO
        isEnable: this.saveData.isEnable, // TODO
        remark: this.saveData.remark, // TODO
        upClueId: this.saveData.upClueId || null, // TODO
        channelType: this.saveData.channelType, // TODO
        sourseAppId: this.saveData.sourseAppId
      };

      // 将新节点名称和父节点id传递到后台
      addClueSource(params).then(res => {
        if (res.resultCode == 200) {
          this.$emit('close', false, true);
        }
      });
    },
    editFunc() {
      this.$refs.formPanel.SUBMIT_FORM();
      let { rowData } = this;
      const reg = /^[A-Z]+$/;
      if (!reg.test(this.saveData.sourseAppId)) {
      }
      let params = {
        id: rowData.id,
        clueLevel: rowData.clueLevel,
        dataSources: rowData.dataSources, //
        ownerCode: rowData.ownerCode, //
        clueName: this.saveData.clueName, // TODO
        dataType: this.saveData.dataType, // TODO
        isEnable: this.saveData.isEnable, // TODO
        remark: this.saveData.remark, // TODO
        upClueId: this.saveData.upClueId, // TODO
        channelType: this.saveData.channelType, // TODO
        sourseAppId: this.saveData.sourseAppId
      };

      // 将新节点名称和父节点id传递到后台
      editClueSource(params).then(res => {
        this.$emit('close', false, true);
      });
    },
    saveHandle() {
      debugger
      this.type === 'EDIT' ? this.editFunc() : this.addFunc();
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    closeHandler() {
      this.$emit('close', false);
    }
  }
};
</script>
<style lang="less" scoped>
</style>
